import axios from 'axios'
import {getRedirectPath} from '../util'
//import { stat } from 'fs';

const ERROR_MSG = 'ERROR_MSG'
const LOAD_DAT = 'LOAD_DAT'
const AUTH_SUCCESS = 'AUTH_SUCCESS'
const LOGOUT = 'LOGOUT'

const initState = {
    redirectTo: '',
    msg: '',
    user: '',
    pwd: '',
    type: ''
}
export function user(state = initState, action) {
    switch (action.type) {
        case AUTH_SUCCESS:
            //console.log(action)
            //console.log(1)
            //console.log(state)
            //console.log(action)
            return {
                ...state,
                msg: '',
                redirectTo: getRedirectPath(action.payload),
                ...action.payload
            }
        case LOAD_DAT:
            return {
                ...state,
                redirectTo: getRedirectPath(action.payload)
            }
        case ERROR_MSG:
            return {
                ...state,
                isAuth: false,
                msg: action.msg
            }
        case LOGOUT:
            return {
                ...initState,
                redirectTo: '/login'
            }
        default:
            return state
    }

    
}

function authSuccess(obj) {
    //console.log('----')
    //console.log(obj)
    const {
        pwd,
        ...data
    } = obj
    return {payload: obj, type: AUTH_SUCCESS}
}

export function loadData(userinfo) {
    return {payload: userinfo, type: LOAD_DAT}
}

function errorMsg(msg) {
    return {msg, type: ERROR_MSG}
}

export function login({user, pwd}) {
    //console.log(user)
    //console.log(pwd)
    
    
    if (!user || !pwd) {
        return errorMsg('用户名密码必须输入')
    }
    return dispath => {
        axios
            .post('/user/login', {user, pwd})
            .then(res => {
                if (res.status === 200 && res.data.code === 0) {
                    //console.log(res.data.data)
                    dispath(authSuccess({...res.data.data}))
                } else {
                    dispath(errorMsg(res.data.msg))
                }
            })
    }
}

export function register({user, pwd, repeatpwd, type}) {
    if (!user || !pwd || !type) {
        return errorMsg('用户名密码必须输入')
    }
    if (pwd !== repeatpwd) {
        return errorMsg('密码和确认密码不同')
    }

    return dispath => {
        axios
            .post('/user/register', {user, pwd, type})
            .then((res) => {
                if (res.status === 200 && res.data.code === 0) {
                    dispath(authSuccess({user, pwd, type}))
                } else {
                    dispath(errorMsg(res.data.msg))
                }
            })
    }

}

export function logoutSubmit() {
    return {type: LOGOUT}
}

export function update(data) {
    return dispath => {
        axios
            .post('/user/update', data)
            .then(res => {
                if (res.status === 200 && res.data.code === 0) {

                    dispath(authSuccess(res.data.data))
                } else {
                    dispath(errorMsg(res.data.msg))
                }
            })
    }
}